Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.YpNetMag = undefined;
var HttpManager = require("AA_HttpManager");
var LogManager = require("AA_LogManager");
var CommonUtils = require("AA_CommonUtils");
var PlayerDataManager = require("AA_PlayerDataManager");
var GameDataManager = require("AA_GameDataManager");
var GameGlobalVariable = require("AA_GameGlobalVariable");
var HallManager = require("AA_HallManager");
var Const_Common = require("AA_Const_Common");
var YPNetStorage = require("AA_YPNetStorage");
var YpNetMag = function () {
  function _creator() {}
  _creator.initServer = function (e) {
    return __awaiter(this, undefined, undefined, function () {
      var t;
      var o;
      return __generator(this, function (n) {
        switch (n.label) {
          case 0:
            if (YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.WX && YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.TT) {
              return [3, 2];
            } else {
              YPNetStorage.YPNetStorage.init();
              t = e.bindingOutUserUid;
              YPNetStorage.YPNetStorage.initServer(YPSDK.appId, YPSDK.platformUrl, t, e);
              o = this;
              return [4, this.getRemoteStorage()];
            }
          case 1:
            o.loadingIn = n.sent();
            if (this.isFail) {
              return [2];
            } else {
              clearTimeout(this.failtimeId);
              if (this.loadingIn) {
                this.initLocaSever();
              } else {
                this.initLoca();
              }
              return [3, 3];
            }
          case 2:
            this.initLoca();
            n.label = 3;
          case 3:
            return [2];
        }
      });
    });
  };
  _creator.failSever = function (e) {
    var t = this;
    this.failtimeId = setTimeout(function () {
      t.initLoca();
      e();
    }, 1e4);
  };
  _creator.initLocaSever = function () {
    var e = Number(cc.sys.localStorage.getItem("gameversion"));
    e || (e = 0);
    var t = this.nowSeverData.gameversion;
    t || (t = 0);
    console.log("存储版本对比", "本地->" + e, "服务器->" + t);
    if (0 == e && t > 0) {
      var o = this.nowSeverData[GameDataManager.GameDataMgr.getSecretKey()];
      var n = this.nowSeverData[PlayerDataManager.PlayerDataMgr.getSecretKey()];
      var i = this.nowSeverData[HallManager.HallMgr.getSecretKey()];
      null == o && CommonUtils.default.SendPoint(Const_Common.SendPointType.数据异常打点, "读取gamedata数据直接为空!!!!");
      null == n && CommonUtils.default.SendPoint(Const_Common.SendPointType.数据异常打点, "读取playerdata数据直接为空!!!!");
      null == i && CommonUtils.default.SendPoint(Const_Common.SendPointType.数据异常打点, "读取halldata数据直接为空!!!!");
    }
    if (t > e) {
      GameDataManager.GameDataMgr.init(this.nowSeverData[GameDataManager.GameDataMgr.getSecretKey()]);
      PlayerDataManager.PlayerDataMgr.init(this.nowSeverData[PlayerDataManager.PlayerDataMgr.getSecretKey()]);
      HallManager.HallMgr.init(this.nowSeverData[HallManager.HallMgr.getSecretKey()]);
      cc.sys.localStorage.setItem("gameversion", t + "");
    } else {
      this.initLoca();
    }
    this.isload = true;
  };
  _creator.initLoca = function () {
    GameDataManager.GameDataMgr.init();
    PlayerDataManager.PlayerDataMgr.init();
    HallManager.HallMgr.init();
    this.isload = true;
  };
  _creator.accesUserinfo = function (e) {
    return __awaiter(this, undefined, Promise, function () {
      return __generator(this, function () {
        return [2, new Promise(function (t) {
          HttpManager.HttpMgr.requestData(function (e) {
            var o;
            LogManager.LogMgr.debug("---accesUserinfo(加钱)-->>>>>>res------", e);
            "Success" == (null === (o = null == e ? undefined : e.errorCode) || undefined === o ? undefined : o.code) && t(e);
          }, e);
        })];
      });
    });
  };
  _creator.getUserAppInfo = function (e, t) {
    return __awaiter(this, undefined, Promise, function () {
      var o = this;
      return __generator(this, function () {
        return [2, new Promise(function (n) {
          var r = YPSDK.platformUrl + "/User/AppInfo?appId=" + e + "platformAppId=" + t + "&code=";
          o.GetTimeAll++;
          o.GetTimeAll < 4 && mg.login({
            success: function (c) {
              return __awaiter(o, undefined, undefined, function () {
                var o;
                return __generator(this, function (i) {
                  switch (i.label) {
                    case 0:
                      console.log("获取code:", c.code);
                      r = YPSDK.platformUrl + "/User/AppInfo?appId=" + e + "&platformAppId=" + t + "&code=" + c.code;
                      console.log("userid", e, t, r);
                      return [4, this.accesUserinfo(r, e, t, c)];
                    case 1:
                    case 2:
                      if (o = i.sent()) {
                        return [3, 4];
                      } else {
                        return [4, this.accesUserinfo(r, e, t, c)];
                      }
                    case 3:
                      o = i.sent();
                      i.label = 4;
                    case 4:
                      console.log("resp:", o);
                      n(o);
                      return [2];
                  }
                });
              });
            },
            fail: function (r) {
              return __awaiter(o, undefined, undefined, function () {
                var o;
                return __generator(this, function (i) {
                  switch (i.label) {
                    case 0:
                      console.error("获取code失败:", r);
                      return [4, this.getUserAppInfo(e, t)];
                    case 1:
                      o = i.sent();
                      n(o);
                      return [2];
                  }
                });
              });
            }
          });
        })];
      });
    });
  };
  _creator.mapSwitchJson = function (e) {
    var t = {};
    e.forEach(function (e, o) {
      t[o] = e;
    });
    return t;
  };
  _creator.cleanData = function (e) {
    cc.sys.localStorage.clear();
    this.saveSeverStorage(true, e);
  };
  _creator.getAllData = function () {
    var e = PlayerDataManager.PlayerDataMgr.getAllData();
    var t = GameDataManager.GameDataMgr.getAllData();
    var o = HallManager.HallMgr.getAllData();
    var n = {};
    n[PlayerDataManager.PlayerDataMgr.getSecretKey()] = this.mapSwitchJson(e);
    n[GameDataManager.GameDataMgr.getSecretKey()] = this.mapSwitchJson(t);
    n[HallManager.HallMgr.getSecretKey()] = this.mapSwitchJson(o);
    n[GameDataManager.GameDataMgr.getSecretKey()] || CommonUtils.default.SendPoint(Const_Common.SendPointType.数据提交异常打点, "提交gamedata数据直接为空!!!!");
    n[PlayerDataManager.PlayerDataMgr.getSecretKey()] || CommonUtils.default.SendPoint(Const_Common.SendPointType.数据提交异常打点, "提交player数据直接为空!!!!");
    n[HallManager.HallMgr.getSecretKey()] || CommonUtils.default.SendPoint(Const_Common.SendPointType.数据提交异常打点, "提交hall数据直接为空!!!!");
    var i = Number(cc.sys.localStorage.getItem("gameversion"));
    i || (i = 0);
    n.gameversion = i;
    return n;
  };
  _creator.isEmpty = function (t) {
    if ("object" == typeof t) {
      var o;
      for (o in t) return false;
      return true;
    }
    return null == t || "null" === t || "undefined" === t || "" === t;
  };
  _creator.transformData = function (e) {
    this.isEmpty(e) && (e = {});
    return JSON.stringify(e);
  };
  _creator.dataFormTrans = function (e) {
    if (this.isEmpty(e)) {
      return {};
    } else {
      return JSON.parse(e);
    }
  };
  _creator.saveSeverStorage = function (e, t) {
    undefined === e && (e = false);
    undefined === t && (t = null);
    return __awaiter(this, undefined, undefined, function () {
      var o;
      var n;
      var i;
      var r;
      return __generator(this, function (a) {
        switch (a.label) {
          case 0:
            if (YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.WX && YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.TT) {
              return [3, 3];
            } else if (this.isload && ((o = new Date().getTime() - this.offtime) > 1e3 * GameGlobalVariable.GameGlobalVariable.severdatatime || e)) {
              this.offtime = new Date().getTime();
              console.log("存档调用时间差", o);
              n = {};
              e || (n = this.getAllData());
              i = this.transformData(n);
              return [4, YPNetStorage.YPNetStorage.setStorage("user", i)];
            } else {
              return [3, 2];
            }
          case 1:
            r = a.sent();
            console.log("存档结果", r, i);
            t && t();
            a.label = 2;
          case 2:
            return [3, 4];
          case 3:
            t && t();
            a.label = 4;
          case 4:
            return [2];
        }
      });
    });
  };
  _creator.getRemoteStorage = function () {
    return __awaiter(this, undefined, undefined, function () {
      var e;
      var t;
      var o;
      var n;
      var i;
      var r;
      return __generator(this, function (a) {
        switch (a.label) {
          case 0:
            e = "user";
            console.log("======>>>>   是否有 本地缓存  ======>>>>", YPNetStorage.YPNetStorage.isHaveLocalCache());
            t = null;
            if (YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.WX && YPSDK.Common.curChannelData.platformType != YPSDK.GamePlatformType.TT) {
              return [3, 6];
            }
            a.label = 1;
          case 1:
            a.trys.push([1, 4,, 5]);
            return [4, this.getUserAppInfo(YPSDK.appId, YPSDK.Common.curChannelData.appId)];
          case 2:
            o = a.sent();
            console.log("获取用户信息", o);
            n = o.data.openid;
            i = o.data.unionid;
            console.log("openid", n);
            console.log("unionid", i);
            YPNetStorage.YPNetStorage.initUseKey(n);
            return [4, YPNetStorage.YPNetStorage.getStorageData(e)];
          case 3:
            t = a.sent();
            console.log("======>>>>   云存档数据-【未登录-设置UserKey-获取】  ======>>>>", t);
            return [3, 5];
          case 4:
            r = a.sent();
            console.log("getRemoteStorage", r);
            return [3, 5];
          case 5:
            return [3, 7];
          case 6:
            cc.sys.isBrowser;
            a.label = 7;
          case 7:
            if (t) {
              this.nowSeverData = this.dataFormTrans(t);
              return [2, true];
            } else {
              return [2, false];
            }
        }
      });
    });
  };
  _creator.loadingIn = false;
  _creator.isload = false;
  _creator.failtimeId = 0;
  _creator.isFail = false;
  _creator.GetTimeAll = 0;
  _creator.offtime = 0;
  _creator.nowSeverData = null;
  return _creator;
}();
exports.YpNetMag = YpNetMag;